<?php

namespace iBrand\Shop\Server\Service;

use Cache;
use iBrand\Component\Discount\Applicators\DiscountApplicator;
use iBrand\Component\Discount\Checkers\ContainsRoleRuleChecker;
use iBrand\Component\Discount\Checkers\CouponEligibilityChecker;
use iBrand\Component\Discount\Checkers\DatesEligibilityChecker;
use iBrand\Component\Discount\Checkers\DiscountEligibilityChecker;
use iBrand\Component\Discount\Checkers\UsageLimitEligibilityChecker;
use iBrand\Component\Discount\Checkers\ContainsCategoryRuleChecker;
use iBrand\Component\Discount\Checkers\ContainsProductRuleChecker;
use iBrand\Component\Discount\Contracts\DiscountContract;
use iBrand\Component\Discount\Contracts\DiscountItemContract;
use iBrand\Component\Discount\Contracts\DiscountSubjectContract;
use iBrand\Component\Discount\Models\Coupon;
use iBrand\Component\Discount\Models\Discount;
use iBrand\Component\Discount\Models\Rule;
use iBrand\Component\Discount\Models\SingleDiscountCondition;
use iBrand\Component\Discount\Repositories\CouponRepository;
use iBrand\Component\Discount\Repositories\DiscountRepository;
use Carbon\Carbon;
use iBrand\Component\Order\Models\Order;
use iBrand\Component\Product\Models\Goods;
use Exception;
use DB;
use Illuminate\Support\Collection;

/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/10/4
 * Time: 21:49
 */
class DiscountService
{
    private $discountRepository;
    private $discountChecker;
    private $couponChecker;
    private $couponRepository;
    protected $applicator;
    protected $datesEligibilityChecker;

    const SINGLE_DISCOUNT_CACHE = 'single_discount_cache';

    public function __construct(DiscountRepository $discountRepository
        , DiscountEligibilityChecker $discountEligibilityChecker
        , CouponRepository $couponRepository
        , CouponEligibilityChecker $couponEligibilityChecker
        , DiscountApplicator $discountApplicator
        , DatesEligibilityChecker $datesEligibilityChecker)
    {
        $this->discountRepository = $discountRepository;
        $this->discountChecker = $discountEligibilityChecker;
        $this->couponRepository = $couponRepository;
        $this->couponChecker = $couponEligibilityChecker;
        $this->applicator = $discountApplicator;
        $this->datesEligibilityChecker = $datesEligibilityChecker;
    }


    /**
     * 根据商品获取所有的优惠折扣，包含促销活动和优惠券，同时过滤是否显示在前端的数据
     *
     * @param DiscountItemContract $item
     */
    public function getDiscountsByGoods(DiscountItemContract $discountItemContract, $channel = 'shop', $is_clearance = 0)
    {
        $collect = collect();
        /*$discounts = empty_collect_cache('o2o_goods_discount_cache', $discountItemContract->id);

        if (!is_null($discounts)) {
            return $discounts;
        }*/


        $discounts = $this->discountRepository->findActive(2, $channel, $is_clearance);

        $discounts = $discounts->filter(function ($item) use ($discountItemContract) {
            return $this->discountChecker->isEligibleItemForO2o($discountItemContract, $item) AND $item->is_open;
        });

        empty_collect_cache([$discountItemContract->id => $discounts], 'o2o_goods_discount_cache', 30);

        if ($discounts instanceof Collection) {
            return $discounts;
        }

        return collect();

    }

}